﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AgilePlannerDT.Data;

namespace AgilePlannerDT.ThirdPartySync
{
    public class APSyncronizer : ISyncronizer
    {
        private Project local;
        private Project foriegn;
        
        public APSyncronizer(Project localProject, Project foriegnProject)
        {
            this.local = localProject;
            this.foriegn = foriegnProject;
        }

        public void updateIteration(Data.Iteration iteration)
        {
            iteration.Update((Iteration)foriegn.FindCard(iteration.SyncID));
        }

        public void updateStoryCard(Data.StoryCard card)
        {
            card.Update((StoryCard)foriegn.FindCard(card.SyncID));
        }

        public void createWorkItem(Data.StoryCard storyCard)
        {
            StoryCard card = (StoryCard)storyCard.Clone();
            card.ID = 0;
            card.Parent = storyCard.ParentCard.SyncID;
            foriegn.CreateStoryCard(card);
            storyCard.SyncID = (int)card.ID;
        }

        public void updateWorkItem(Data.StoryCard storyCard)
        {
            StoryCard foriegnCard = (StoryCard)foriegn.FindCard(storyCard.SyncID);
            long id = foriegnCard.ID;
            foriegnCard.Update(storyCard);
            foriegnCard.ID = id;
            foriegnCard.SyncID = (int)storyCard.ID;
        }

        public void deleteWorkItem(Data.StoryCard storyCard)
        {
            foriegn.DeleteCard(storyCard.SyncID);
        }

        public void createTimeBox(Data.Iteration iteration)
        {
            Iteration timeBox = (Iteration)iteration.Clone();
            timeBox.ID = 0;
            timeBox.SyncID = (int)iteration.ID;

            foriegn.AddIteration(timeBox);
        }

        public void updateTimeBox(Data.Iteration iteration)
        {
            Iteration timebox = (Iteration)foriegn.FindCard(iteration.SyncID);
            long id = timebox.ID;
            timebox.Update(iteration);
            timebox.ID = id;
            timebox.SyncID = (int)iteration.ID;
        }

        public void deleteTimeBox(Data.Iteration iteration)
        {
            foriegn.DeleteCard(iteration.SyncID);
        }

        public List<Data.StoryCard> getActiveWorkItems()
        {
            List<StoryCard> cards = new List<StoryCard>();
            foreach (StoryCard card in foriegn.Backlog.StoryCardChildren)
            {
                if (!card.Status.Equals("Accepted") && !card.Status.Equals("Complete"))
                {
                    StoryCard card2 = (StoryCard)card.Clone();
                    card2.SyncID = (int)card.ID;
                    card2.ID = 0;
                    card2.Parent = 0;
                    cards.Add(card2);
                }
            }
            foreach (Iteration iter in foriegn.IterationChildren)
            {
                foreach (StoryCard card in iter.StoryCardChildren)
                {
                    if (!card.Status.Equals("Accepted") && !card.Status.Equals("Complete"))
                    {
                        StoryCard card2 = (StoryCard)card.Clone();
                        card2.SyncID = (int)card.ID;
                        card2.ID = 0;
                        card2.Parent = 0;
                        foreach(Iteration iteration in local.IterationChildren){
                            if (iteration.SyncID == iter.ID)
                            {
                                card2.Parent = iteration.ID;
                            }
                        }
                        cards.Add(card2);
                    }
                }
            }
            return cards;
        }

        public List<Data.Iteration> getActiveIterations()
        {
            List<Iteration> iterations = new List<Iteration>();
            foreach (Iteration timebox in foriegn.IterationChildren)
            {
                Iteration clone = (Iteration)timebox.Clone();
                clone.SyncID = (int)timebox.ID;
                clone.ID = 0;
                clone.StoryCardChildren.Clear();
                iterations.Add(clone);
            }
            return iterations;
        }

        public bool contains(Data.IndexCard card)
        {
            if (card.SyncID <= 0) return false;
            try
            {
                foriegn.FindCard(card.SyncID);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
